home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-04-24 | 5.0 KB | 188 lines | [TEXT/CWIE] |
- /* -------------------------------------------------------------
- This applet paints a circle or square of the color you've chosen
- wherever you click. Every second, it blinks the shape to yellow.
- All shapes blink independently of each other.
-
- This applet keeps a list of the shapes you've drawn
- and paints all the shapes in the list when it repaints.
-
- Java's classes: Applet (applet)
- Event (awt) user-generated action
- Graphics (awt) used for drawing
- Color (awt) defines colors
- Choice (awt) shape and color selection choices
- Vector (util) list of shapes
- Thread (lang)
-
- Custom classes: SimpleDraw
- Circle defines and draws circles
- Square defines and draws squares
- Shape a common ancestor for circles and squares
- BlinkThread controls drawing for a shape
-
- ------------------------------------------------------------- */
-
- import java.applet.Applet;
- import java.util.*;
- import java.awt.*;
-
- public class SimpleDraw extends Applet {
- Vector threads;
- Choice shapeChoice;
- Choice colorChoice;
-
- /** Create the GUI. */
- public void init() {
- threads = new Vector();
-
- shapeChoice = new Choice();
- shapeChoice.addItem("Circle");
- shapeChoice.addItem("Square");
- add(shapeChoice);
-
- colorChoice = new Choice();
- colorChoice.addItem("Red");
- colorChoice.addItem("Green");
- colorChoice.addItem("Blue");
- add(colorChoice);
-
- BlinkThread.g = getGraphics(); // Get the graphics object for the applet
- }
-
- /** Create a new shape. */
- public boolean mouseUp(Event e, int x, int y) {
-
- BlinkThread t;
- Shape s; // This shape will be either a circle or a square.
-
- String shapeString = shapeChoice.getSelectedItem();
- String colorString = colorChoice.getSelectedItem();
-
- if (shapeString.equals("Circle"))
- s = new Circle();
- else
- s = new Square();
-
- if (colorString.equals("Red"))
- s.color = Color.red;
- else if (colorString.equals("Green"))
- s.color = Color.green;
- else
- s.color = Color.blue;
-
- s.x = x;
- s.y = y;
-
- t = new BlinkThread(s);
- t.start();
- threads.addElement(t);
-
- return true;
- }
-
- /** Resume all the threads when the applet starts. */
- public void start() {
- BlinkThread t;
- int numThreads;
-
- numThreads = threads.size();
- for (int i = 0; i < numThreads; i++) {
-
- t = (BlinkThread)threads.elementAt(i);
- t.resume();
- }
- }
-
- /** Suspend all the threads when the applet stops. */
- public void stop() {
- BlinkThread t;
- int numThreads;
-
- numThreads = threads.size();
- for (int i = 0; i < numThreads; i++) {
-
- t = (BlinkThread)threads.elementAt(i);
- t.suspend();
- }
- }
-
- /** Stop all the threads when the applet goes away. */
- public void destroy() {
- BlinkThread t;
- int numThreads;
-
- numThreads = threads.size();
- for (int i = 0; i < numThreads; i++) {
-
- t = (BlinkThread)threads.elementAt(i);
- t.stop();
- }
- }
- }
-
- /** Shapes provide common characteristics for the circle and square. */
- abstract class Shape {
- static public final int shapeRadius = 20;
-
- Color color;
- int x;
- int y;
-
- abstract void draw(Graphics g);
- abstract void drawBlink(Graphics g);
- }
-
- /** Draws and maintains circle information. */
- class Circle extends Shape {
- void drawBlink(Graphics g) {
- g.setColor(Color.yellow);
- g.fillOval(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);
- }
-
- void draw(Graphics g) {
- g.setColor(this.color);
- g.fillOval(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);
- }
- }
-
- /** Draws and maintains square information. */
- class Square extends Shape{
- void drawBlink(Graphics g) {
- g.setColor(Color.yellow);
- g.fillRect(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);
- }
-
- void draw(Graphics g) {
- g.setColor(this.color);
- g.fillRect(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);
- }
- }
-
- /** Thread to control when to blink a shape. */
- class BlinkThread extends Thread {
- static Graphics g;
- Shape s;
-
- BlinkThread(Shape s) {
- this.s = s;
- }
-
- public void run() {
-
- // don't ever exit the thread
- while(true) {
-
- try {
- s.drawBlink(g);
- sleep(250); // Go to sleep for a 1/4 of a second (250 milliseconds)
-
- s.draw(g);
- sleep(1000); // Go to sleep for 1 second (1000 milliseconds)
-
- } catch (Exception e) {
- }
-
- }
- }
- }
-